package gui.newQuiz;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import quiz.Question;

public class SetupQuestions extends JFrame implements ActionListener {
	Question question;
	JButton okButton = new JButton("Ok");
	List<List<JTextField>> rows;
	
	JLabel pointsLabel;
	JLabel helpLabel;
	JTextField pointsText;
	JTextField helpText;
	

	public SetupQuestions(Question question) {
		this.question = question;
		initializeVariables();
		setupWindows();	
	}

	private void initializeVariables() {
		rows = new ArrayList<List<JTextField>>();
		
		pointsLabel = new JLabel("Points");
		helpLabel = new JLabel("Value");
		pointsText = new JTextField();
		helpText = new JTextField();
	}

	private void addActionListeners() {
		okButton.addActionListener(this);
	}

	private void setupWindows() {
		GridLayout gr = new GridLayout();
		getContentPane().setLayout(
				new GridLayout(question.getNumberOfSubQuestions(), 8));

		for (int i = 0; i < question.getNumberOfSubQuestions(); i++) {
			// TODO Refactor this. Please.
			List<JTextField> questionComponents = new ArrayList<JTextField>();
			for (int j = 0; j < 2; j++) {
				JTextField emptyTextField = new JTextField();
				JLabel questionLabel = null;
				if (j == 0) {
					questionLabel = new JLabel("Question");
				} else if (j == 1) {
					questionLabel = new JLabel("Answer");
				}
				
				//hmsasda
				getContentPane().add(questionLabel);
				getContentPane().add(emptyTextField);
				questionComponents.add(emptyTextField);
			}

			rows.add(questionComponents);
		}
		getContentPane().add(pointsLabel);
		getContentPane().add(pointsText);
		getContentPane().add(helpLabel);
		getContentPane().add(helpText);
		getContentPane().add(okButton);
		addActionListeners();
		
		setPreferredSize(new Dimension(400, 400));
		setVisible(true);
		pack();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(okButton)) {
			
			String tmpQuestion = "";
			String tmpSolution = "";
			
			for(List<JTextField> questions : rows) {
				tmpQuestion += questions.get(0).getText() + " - ";
				tmpSolution += questions.get(1).getText() + " - ";
			}
			
			question.setQuestion(tmpQuestion);
			question.setSolution(tmpSolution);
			question.setHelp(helpText.getText());
			question.setValue(Integer.parseInt(pointsText.getText()));
			dispose();
		}
	}
}
